package com.example;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

public class PhantomReferenceTest {

    public static class Car{

        @Override
        public String toString() {
            return "这是一辆车";
        }
    }

    public static void main(String[] args) {
        Car car = new Car();
        ReferenceQueue referenceQueue = new ReferenceQueue();
        PhantomReference<Car> phantomReference = new PhantomReference<>(car, referenceQueue);

        while (true){
            car = null;
            PhantomReference temp = (PhantomReference) referenceQueue.poll();
            if(temp != null){
                System.out.println("car挂了");
                System.out.println("temp == phantomReference?: " + (temp == phantomReference));
                break;
            }else {
                System.out.println("car还没挂");
            }
        }
    }
}
